package com.yu.waterstation.app.common.util.session;

import com.yu.waterstation.common.entity.app.dto.StudentDTO;
import com.yu.waterstation.common.entity.app.dto.WorkerDTO;

import com.yu.waterstation.common.common.constant.Const;
import com.yu.waterstation.common.common.util.GeneratorUtil;
import com.yu.waterstation.common.common.util.jwt.JWTUtil;
import com.yu.waterstation.common.common.util.spring.SpringContextUtil;
import com.yu.waterstation.common.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * session服务
 */
@Slf4j
public class SessionUtil {
    private static final RedisService redisService = SpringContextUtil.getBeanByClass(RedisService.class);

    //******************* student session start ****************//
    // 学生：退出登录
    public static boolean studentLogout(HttpServletRequest request) {
        return redisService.hdel(Const.CONST_student_session_map, getStudentIdFromRequest(request).toString());
    }

    // 学生：是否登录
    public static boolean checkStudentLogin(Integer studentId) {
        return redisService.hHasKey(Const.CONST_student_session_map, studentId.toString());
    }

    // 学生：添加会话缓存
    public static void setStudentSession(StudentDTO studentDTO) {
        long expireTime = GeneratorUtil.generateExpireTime(Const.CONST_half_month);
        studentDTO.setToken(JWTUtil.generateTokenForAppUser(studentDTO.getId(), expireTime));
        redisService.hset(Const.CONST_student_session_map, studentDTO.getId().toString(), "" + expireTime);
    }

    // 当前学生的的id 前提是已经登录了
    public static Integer getStudentIdFromRequest(HttpServletRequest request) {
        return (Integer) request.getAttribute(Const.CONST_student_id);
    }
    //******************* student session end ****************//


    //******************* worker session start ****************//
    // 送水员：退出登录
    public static void workerLogout(HttpServletRequest request) {
        redisService.hdel(Const.CONST_worker_session_map, request.getAttribute(Const.CONST_worker_id).toString());
    }

    // 送水员：是否登录
    public static boolean checkWorkerLogin(Integer workerId) {
        return redisService.hHasKey(Const.CONST_worker_session_map, workerId.toString());
    }

    // 送水员：添加会话缓存
    public static void setWorkerSession(WorkerDTO workerDTO) {
        long expireTime = GeneratorUtil.generateExpireTime(Const.CONST_half_month);
        workerDTO.setToken(JWTUtil.generateTokenForAppUser(workerDTO.getId(), expireTime));
        redisService.hset(Const.CONST_worker_session_map, workerDTO.getId().toString(), "" + expireTime);
    }

    // 送水员的id 前提是已经登录了
    public static Integer getWorkerIdFromRequest(HttpServletRequest request) {
        return (Integer) request.getAttribute(Const.CONST_worker_id);
    }
    //******************* worker session end *****************//


    // 找请求里携带的token
    public static String getToken(HttpServletRequest request) {
        String token = request.getHeader(Const.CONST_token); // 从header里面找
        if (StringUtils.isEmpty(token)) // 从url后面的参数里找
            token = request.getParameter(Const.CONST_token);
        return token;
    }
}
